package com.h2t.loveplace.views;

import com.h2t.loveplace.main.LoveplaceMidlet;
import com.h2t.loveplace.utils.Storage;
import com.sun.lwuit.Button;
import com.sun.lwuit.Command;
import com.sun.lwuit.Container;
import com.sun.lwuit.Display;
import com.sun.lwuit.Form;
import com.sun.lwuit.Image;
import com.sun.lwuit.Label;
import com.sun.lwuit.VideoComponent;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.layouts.BorderLayout;
import com.sun.lwuit.layouts.BoxLayout;
import com.sun.lwuit.layouts.FlowLayout;
import com.sun.lwuit.layouts.Layout;
import java.io.IOException;
import javax.microedition.media.*;
import javax.microedition.media.control.VideoControl;

public class CameraCapture implements ActionListener {

    private Form form, savePhotoForm;
    private VideoComponent videoComponent;
    private VideoControl videoControl;
    private Command capture, saveCmd, cmdBack;
    private LoveplaceMidlet midlet;
    private Image capturedImage;
    private Button btnSave, btnRetake;
    private Label displayPhoto;
    private byte[] raw;
//    CameraThread camera = null;
    private Player player;

    // CameraCapture parent;
    public CameraCapture(LoveplaceMidlet midlet) {
        this.midlet = midlet;
        form = new Form("Camera");
        saveCmd = new Command("Save");
//        retakeCmd = new Command("Retake");
        savePhotoForm = new Form("Save photo");
        cmdBack = new Command("Back");
//        snapshot = new Button("Snapshot");
//        snapshot.addActionListener(this);
        capture = new Command("Capture");
        btnSave = new Button("Save");
        btnSave.addActionListener(saveListener);
//        btnRetake = new Button("Retake");
//        btnRetake.addActionListener(saveListener);
        savePhotoForm.setLayout(new BorderLayout());
        savePhotoForm.addCommand(saveCmd);
//        savePhotoForm.addCommand(retakeCmd);
        savePhotoForm.addCommand(cmdBack);
        savePhotoForm.setBackCommand(cmdBack);
        savePhotoForm.addCommandListener(saveListener);
        displayPhoto = new Label();
        savePhotoForm.addComponent(BorderLayout.CENTER, displayPhoto);

        Container buttonContainer = new Container(new BoxLayout(BoxLayout.X_AXIS));
        buttonContainer.addComponent(btnSave);
//        buttonContainer.addComponent(btnRetake);
        savePhotoForm.addComponent(BorderLayout.SOUTH, buttonContainer);

        form.addCommand(capture);
        form.setBackCommand(cmdBack);
        form.addCommand(cmdBack);
        form.setLayout(new FlowLayout());
//        form.addComponent(snapshot);
        form.addCommandListener(this);

//        createCamera();
    }
    private ActionListener saveListener = new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            form.repaint();
            savePhotoForm.repaint();
            if (ae.getSource() == btnSave) { // save image to file{
                if (raw.length > 0) {
                    String fileName = Storage.storeImg(raw);
                    System.out.println("file name " + fileName);
                }
            } else if (ae.getSource() == btnRetake) { // take another photos
            }
//            if (ae.getCommand() == saveCmd) {
//                if (raw.length > 0) {
//                    String fileName = Storage.storeImg(raw);
//                    camera.interrupt();
//                    close();
//                    System.out.println("file name " + fileName);
//                    midlet.filename = fileName;
//
//                }
//            }
//            if (ae.getCommand() == retakeCmd) {
//                createCamera();
//            }
            if (ae.getCommand() == cmdBack) {
//                close();
//                midlet.showNewPlaceView();
            }
        }
    };

    public void createCamera() {
        try {
            videoComponent = VideoComponent.createVideoPeer("capture://image");
            player = (Player) videoComponent.getNativePeer();
            player.realize();
            videoControl = (VideoControl) player.getControl("VideoControl");
            videoComponent.setFullScreen(false);
            form.addComponent(videoComponent);
            videoComponent.setPreferredH(Display.getInstance().getDisplayHeight());
            videoComponent.setPreferredW(Display.getInstance().getDisplayWidth());
            videoComponent.start();
            //  System.out.println("create camera , fullscreen : " + videoComponent.isFullScreen());
            form.show();
        } catch (MediaException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public void actionPerformed(ActionEvent ae) {
        if (ae.getCommand() == capture) {
            // CameraThread 
//                camera = new CameraThread();
//                camera.start();
            takeSnapshot();
        }
    }

//    void close() {
//        if (videoComponent != null) {
//            videoComponent.stop();
////            try {
////                player.stop();
////            } catch (MediaException ex) {
////                ex.printStackTrace();
////            }
////            player.close();
//            camera.interrupt();
//            camera = null;
//        }
//        videoComponent = null;
//        videoControl = null;
//
//    }
//    class CameraThread extends Thread {
//
//        //   private CameraCapture parent;
//        private boolean stop = false;
//
//        public boolean isStop() {
//            return stop;
//        }
//
//        public void setStop(boolean stop) {
//            this.stop = stop;
//        }
//
//        public void run() {
//            //   while (!stop) {
//
//            takeSnapshot();
//            //  }
//        }
//
//        public void takeSnapshot() {
//            try {
//                raw = videoControl.getSnapshot(null);
//                capturedImage = Image.createImage(raw, 0, raw.length);
//                displayPhoto.setIcon(capturedImage);
//                videoComponent.stop();
//
//                form.removeComponent(videoComponent);
//                videoComponent = null;
//                videoControl = null;
//                String fileName = Storage.storeImg(raw);
//                System.out.println("file name " + fileName);
//                midlet.filename = fileName;
//                form.removeAll();
//                form.setVisible(false);
//                player.stop();
//                stop = true;
//                player = null;
//                close();
//
//                midlet.showNewPlaceView();
//                //savePhotoForm.show();
//            } catch (MediaException ex) {
//                ex.printStackTrace();
//            }
//        }
//    }
    private void takeSnapshot() {
        try {
            raw = videoControl.getSnapshot(null);
            capturedImage = Image.createImage(raw, 0, raw.length);
//                displayPhoto.setIcon(capturedImage);
            if (raw.length > 0) {
                String fileName = Storage.storeImg(raw);
                System.out.println("file name " + fileName);
                midlet.filename = fileName;
            }
            player.stop();
            videoComponent.stop();
            player.close();
            midlet.showNewPlaceView();
        } catch (MediaException ex) {
            ex.printStackTrace();
        }

//
    }
}
